#include <QtGui/QGraphicsProxyWidget>
#include "mainui.h"
#include "ui_mainui.h"
#include "qrencode.h"

mainUi::mainUi(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mainUi)
{
    ui->setupUi(this);
    ui->iconM->setPixmap(QPixmap("/root/pic/machine.png"));
    ui->iconN->setPixmap(QPixmap("/root/pic/wireless.png"));
    ui->operation->setPixmap(QPixmap("/root/pic/ico.jpg"));
    ui->guanggao->setPixmap(QPixmap("/root/pic/timg.jpg"));
    // ui->iconM->clear();
    // ui->iconM->setPixmap(QPixmap::fromImage(image));
    // image.load("/root/pic/wireless.png");
    // ui->iconN->clear();
    // ui->iconN->setPixmap(QPixmap::fromImage(image));
    // image.load("/root/pic/ico.jpg");
    // ui->operation->clear();
    // ui->operation->setPixmap(QPixmap::fromImage(image));
    // image.load("/root/pic/timg.jpg");
    // ui->guanggao->clear();
    // ui->guanggao->setPixmap(QPixmap::fromImage(image));

    QGraphicsScene *sce = new QGraphicsScene(this);

    lblMachineId = new QLabel();
    lblMachineId->setFont(QFont("Arial", 20));
    QGraphicsProxyWidget *w = sce->addWidget(lblMachineId);
    if (w != 0)
    {
        w->setRotation(270);
    }
    ui->machineId->setStyleSheet("background: transparent; border:0px");
    ui->machineId->setScene(sce);

    QGraphicsScene *sceStatus = new QGraphicsScene(this);
    lblStatus = new QLabel();
    lblStatus->setFont(QFont("Arial", 20));
    QGraphicsProxyWidget *wStatus = sceStatus->addWidget(lblStatus);
    if (wStatus != 0)
    {
        wStatus->setRotation(270);
    }
    ui->status->setStyleSheet("background: transparent; border:0px");
    ui->status->setScene(sceStatus);

    QGraphicsScene *sceNetStatus = new QGraphicsScene(this);
    lblNetStatus = new QLabel();
    lblNetStatus->setFont(QFont("Arial", 20));
    QGraphicsProxyWidget *wNetStatus = sceNetStatus->addWidget(lblNetStatus);
    if (wNetStatus != 0)
    {
        wNetStatus->setRotation(270);
    }
    ui->netstatus->setStyleSheet("background: transparent; border:0px");
    ui->netstatus->setScene(sceNetStatus);

    setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint );

    QObject::connect(this,SIGNAL(netPicChange(int)),this,SLOT(changeBackPic(int)),Qt::QueuedConnection);
    QObject::connect(this,SIGNAL(qrcodeUpdate(QString)),this,SLOT(changeQrCode(QString)),Qt::QueuedConnection);
}

void mainUi::setInfo(QString machineOnline)
{
    lblMachineId->setText(machineOnline);
    lblMachineId->adjustSize();
}

void mainUi::updateQrCodeContent(QString qUrl)
{
    emit this->qrcodeUpdate(qUrl);
}

void mainUi::setStatus(QString status)
{
    lblStatus->setText(status);
    lblStatus->adjustSize();
}

void mainUi::changeQrCode(QString qUrl)
{
#if 1
    QRcode *imgqrcode; //
    //QR_ECLEVEL_Q
    imgqrcode = QRcode_encodeString(qUrl.toStdString().c_str(), 2, QR_ECLEVEL_Q, QR_MODE_8, 1);
    qint32 temp_width=ui->qrcode->width(); //
    qint32 temp_height=ui->qrcode->height();
    qint32 qrcode_width = imgqrcode->width > 0 ? imgqrcode->width : 1;
    double scale_x = (double)temp_width / (double)qrcode_width; //
    double scale_y =(double) temp_height /(double) qrcode_width;
    QImage mainimg=QImage(temp_width,temp_height,QImage::Format_ARGB32);
    QPainter painter(&mainimg);
    QColor background(Qt::white);
    painter.setBrush(background);
    painter.setPen(Qt::NoPen);
    painter.drawRect(0, 0, temp_width, temp_height);
    QColor foreground(Qt::black);
    painter.setBrush(foreground);
    for( qint32 y = 0; y < qrcode_width; y ++)
    {
        for(qint32 x = 0; x < qrcode_width; x++)
        {
            unsigned char b = imgqrcode->data[y * qrcode_width + x];
            if(b & 0x01)
            {
                QRectF r(x * scale_x, y * scale_y, scale_x, scale_y);
                painter.drawRects(&r, 1);
            }
        }
    }
    QPixmap mainmap=QPixmap::fromImage(mainimg);
    ui->qrcode->clear();
    ui->qrcode->setPixmap(mainmap);
    ui->qrcode->setVisible(true);
    ui->qrcode->raise();
#endif
}

void mainUi::changeBackPic(int iStatus)
{
    QImage image;
    if(iStatus == 1)
    {
        image.load("/root/pic/online.png");
        ui->iconN->clear();
        ui->iconN->setPixmap(QPixmap::fromImage(image));
    }
    else
    {
        image.load("/root/pic/offline.png");
        ui->iconN->clear();
        ui->iconN->setPixmap(QPixmap::fromImage(image));
        //ui->iconN->setPixmap(QPixmap("/root/pic/offline.png"));
    }
}

void mainUi::setNetStatus(QString netStatus)
{
    //online
    if (netStatus == "1")
    {
        lblNetStatus->setText("Online   ");
        lblNetStatus->setStyleSheet("color:green;");
		lblNetStatus->adjustSize();
        //iconN->setPixmap(QPixmap("/root/pic/online.png"));
		emit this->netPicChange(1);
    }
    else if (netStatus == "0")
    {
        lblNetStatus->setText("Offline  ");
        lblNetStatus->setStyleSheet("color:red;");
		lblNetStatus->adjustSize();
        //iconN->setPixmap(QPixmap("/root/pic/offline.png"));
		emit this->netPicChange(0);
    }
	else
	{
		lblNetStatus->setText("Waitting ");
        lblNetStatus->setStyleSheet("color:black;");
		lblNetStatus->adjustSize();
		emit this->netPicChange(0);
        //iconN->setPixmap(QPixmap("/root/pic/offline.png"));
	}
}

mainUi::~mainUi()
{
    delete ui;
}
